
'use strict'

const bcrypt = require('bcrypt-promise')
// const fs = require('fs-extra')
const mime = require('mime-types');
const sqlite = require('sqlite-async');
const dbUnit = require('../lib/dbUnit');
const hosttool = require('../lib/hosttool');

const saltRounds = 10;

module.exports = class User {

	constructor(dbName = 'website.db') {
		return (async() => {
			this.db = await sqlite.open(dbName)
			// we need this table to store the user accounts
			const sql = 'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, pass TEXT);'
			await this.db.run(sql)
			return this
		})()
	}

	async register(user, pass, email, avatarUrl) {
		try {
			if(user.length === 0) throw new Error('missing username')
			if(pass.length === 0) throw new Error('missing password')

			let sql = `SELECT COUNT(id) as records FROM users WHERE user= ? ;`
			const data = await dbUnit.getRecordAsync(sql, user); // this.db.get(sql)

			if (data.records !== 0) {
				throw new Error(`username "${user}" already in use`);
			}

			let dateStr = hosttool.getNowTimeStr();
			pass = await bcrypt.hash(pass, saltRounds);
			sql = `INSERT INTO users(user, pass, email, avatarUrl, intime) 
			         VALUES($user, $pass, $email, $avatarUrl, $dateStr)`;
			await this.db.run(sql, {
				$user: user,
				$pass: pass,
				$email: email,
				$avatarUrl: avatarUrl,
				$dateStr: dateStr
			});
			return true;
		} catch(err) {
			throw err
		}
	}

	// async uploadPicture(path, mimeType) {
	// 	const extension = mime.extension(mimeType)
	// 	console.log(`path: ${path}`)
	// 	console.log(`extension: ${extension}`)
	// 	//await fs.copy(path, `public/avatars/${username}.${fileExtension}`)
	// }

	async login(username, password) {
		try {
			let sql = `SELECT count(id) AS count FROM users WHERE user= ?;`
			const records = await this.db.get(sql, username)
			if(!records.count) throw new Error(`username "${username}" not found`)

			sql = `SELECT id,user,pass,email,avatarUrl FROM users WHERE user = ?;`
			const record = await this.db.get(sql, username)
			const valid = await bcrypt.compare(password, record.pass)
			if(valid === false) throw new Error(`invalid password for account "${username}"`)
			return record;
		} catch(err) {
			throw err
		}
	}

}
